package top.qrcodepay.plugin.map;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.imageio.ImageIO;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.map.MapView;
import org.bukkit.plugin.java.JavaPlugin;

import top.qrcodepay.plugin.QrcodePay;

public class MapGenerator {

	private static File listFile = new File(JavaPlugin.getPlugin(QrcodePay.class).getDataFolder(), "list.yml");
	
	public static YamlConfiguration list = getList();
	
	@SuppressWarnings("deprecation")
	public static void generate(int appId, String playerName) {
		short mapId = (short) list.getInt(playerName);
		MapView map = Bukkit.getMap(mapId);
		if (mapId == 0 || map == null) {
			mapId = getNewMapId();
			list.set(playerName, mapId);
			saveList();
		}
		QrcodeRenderer.installRenderer(getQrcodeImage(appId, playerName), map);
		ItemStack item = new ItemStack(Material.MAP, 1, (byte) mapId);
		ItemMeta itemMeta = item.getItemMeta();
		itemMeta.setDisplayName("§6§l【§c§l" + playerName + "§6§l】的充值二维码");
		List<String> lore = new ArrayList<>();
		lore.add("§a本二维码为【§b" + playerName + "§a】的专属充值二维码");
		lore.add("§a请在确认是您要充值的账号后使用");
		itemMeta.setLore(lore);
		item.setItemMeta(itemMeta);
		Player player = Bukkit.getPlayer(playerName);
		HashMap<Integer,ItemStack> leftItems = player.getInventory().addItem(item);
		if(leftItems.isEmpty()){
			player.sendMessage("§6§l欢迎使用二维码充值功能，您的二维码已放在您的背包中，请打开查看！");
		} else {
			player.sendMessage("§6§l抱歉，您的背包已满，请清理后重试！");
		}
	}
	
	public static void saveList() {
		try {
			list.options().copyDefaults(true);
			list.save(listFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static YamlConfiguration getList() {
		if (!listFile.exists()) {
			try {
				listFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return YamlConfiguration.loadConfiguration(listFile);
	}
	
	@SuppressWarnings("deprecation")
	public static short getNewMapId() {
		short newId = Bukkit.createMap(Bukkit.getWorlds().get(0)).getId();
		if (newId == 0) newId = Bukkit.createMap(Bukkit.getWorlds().get(0)).getId();
		return newId;
	}
	
	public static BufferedImage getQrcodeImage(int appId, String playerName) {
		BufferedImage image = null;
		try {
			URL url = new URL("http://qr.liantu.com/api.php?w=128&m=3&text=http://qrcodepay.top/pay?a=" + appId + "%26n=" + playerName);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(5000);
			InputStream stream = connection.getInputStream();
			image = ImageIO.read(stream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return image;
	}
	
}
